package com.qunar.wan.service;

import com.google.common.base.Preconditions;
import com.qunar.wan.common_api.model.Classify;
import com.qunar.wan.common_api.model.Query;
import com.qunar.wan.common_api.model.SubClassify;
import com.qunar.wan.common_api.service.AdminLogService;
import com.qunar.wan.common_api.service.CacheService;
import com.qunar.wan.common_api.service.ClassifyService;
import com.qunar.wan.common_api.service.SubClassifyService;
import com.qunar.wan.common_api.service.mapper.SubClassifyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: hangyu.shen Date: 13-11-26 Time: 上午11:32
 */

@Service("subClassifyService")
public class SubClassifyServiceImpl extends BaseServiceImpl<SubClassify, SubClassifyMapper> implements
        SubClassifyService {
    @Resource
    private ClassifyService classifyService;
    @Resource
    private AdminLogService adminLogService;
    @Resource
    private CacheService cacheService;

    private static final String INGNORE_ALIVE = "YES";

    @Override
    public List<SubClassify> selectListBySupId(int supId) {
        Preconditions.checkArgument(supId > 0, "(SubClassifyServiceImpl)supId must greater than 0, your input is: "
                + supId);
        List<SubClassify> list = selectByQuery(new Query().fill("supId", supId));
        return list;
    }

    @Override
    public List<SubClassify> selectListBySubName(String subName) {
        Preconditions.checkNotNull(subName);
        List<SubClassify> list = selectByQuery(new Query().fill("subName", subName));
        return list;
    }

    @Override
    public SubClassify selectSubClassifyById(int id, boolean alive) {
        Preconditions.checkArgument(id > 0, "(SubClassifyServiceImpl)id must greater than 0, your input is: " + id);
        Query query = new Query().fill("id", id);
        if (!alive) {
            query.fill("alive", INGNORE_ALIVE);
        }
        List<SubClassify> subClassify = selectByQuery(query);
        return subClassify.isEmpty() ? null : subClassify.get(0);
    }

    @Override
    public void insertSubClassify(int supId, String subName) {
        Preconditions.checkArgument(supId > 0, "(SubClassifyServiceImpl)supId must greater than 0, your input is: "
                + supId);
        Preconditions.checkNotNull(subName);
        SubClassify subClassify = new SubClassify();
        subClassify.setSupId(supId);
        subClassify.setSubName(subName);
        insertObj(subClassify);
        cacheService.refreshSubsCache(subClassify.getSupId());
        cacheService.refreshAllClassisfiesCache();
    }

    @Override
    public void deleteSubClassifyById(int id) {
        Preconditions.checkArgument(id > 0, "(SubClassifyServiceImpl)id must greater than 0, your input is: " + id);
        SubClassify subClassify = selectSubClassifyById(id, false);
        deleteByPK(subClassify);
        cacheService.refreshSingleSubClassifyCache(id);
        cacheService.refreshSubsCache(subClassify.getSupId());
        cacheService.refreshAllClassisfiesCache();

    }

    @Override
    public void insertSubClassifyByTran(int adminId, int supId, String subName) {
        Preconditions.checkArgument(adminId > 0, "(SubClassifyServiceImpl)adminId must greater than 0, your input is: "
                + adminId);
        Preconditions.checkArgument(supId > 0, "(SubClassifyServiceImpl)supId must greater than 0, your input is: "
                + supId);
        Preconditions.checkNotNull(subName);
        Classify classify = classifyService.selectClassifyById(supId);
        String str = String.format("添加'%s'子分类活动到'%s'大分类下", subName, classify.getClaName());
        adminLogService.insertAdminLog(adminId, str);
        insertSubClassify(supId, subName);
    }

    @Override
    public void deleteSubClassifyByTran(int adminId, int supId, int id) {
        Preconditions.checkArgument(adminId > 0, "(SubClassifyServiceImpl)adminId must greater than 0, your input is: "
                + adminId);
        Preconditions.checkArgument(supId > 0, "(SubClassifyServiceImpl)supId must greater than 0, your input is: "
                + supId);
        Preconditions.checkArgument(id > 0, "(SubClassifyServiceImpl)id must greater than 0, your input is: " + id);
        Classify classify = classifyService.selectClassifyById(supId);
        SubClassify subClassify = selectSubClassifyById(id, true);
        String str = String.format("删除'%s'大分类活动下的'%s'子分类活动", classify.getClaName(), subClassify.getSubName());
        adminLogService.insertAdminLog(adminId, str);
        deleteSubClassifyById(id);
    }
}
